<!-- 找回密码 -->
<template>
	<view class="form">
		<view style="display: block; width: 100%;">
			<u-form-item border="true" label="用户名:" label-width="140">
				<u-input v-model="username" id="请输入用户名" />
			</u-form-item>
			<!-- <u-form-item border="true" label="性别:" label-width="140">
				<u-radio-group v-model="value" @change="radioGroupChange">
					<u-radio v-for="(item, index) in list" :key="index" :name="item.name"
						:disabled="item.disabled">
						{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item> -->
			<u-form-item border="true" label="旧密码:" label-width="140">
				<u-input v-model="oldPassword" type="password" placeholder="请输入原始密码" />
			</u-form-item>
			<u-form-item border="true" label="新密码:" label-width="140">
				<u-input v-model="newPassword" type="password" placeholder="请输入新密码" />
			</u-form-item>
			<u-form-item border="true" label="确认密码:" label-width="140">
				<u-input v-model="rePassword" type="password" placeholder="请确认密码" />
			</u-form-item>
		</view>
		<view>
			<u-button type="primary" style="width: 300px;" class="u-margin-top-40" @click="modifypass">修改密码</u-button>
		</view>
	</view>
</template>

<script>
	import request from '../../util/request.js'
	import PasswordRequest from '../../util/PasswordRequest.js'
	import api from '../../util/api.js'
	export default {
		data() {
			return {
				value: '男',
				list: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					}
				],
				oldPassword: '',
				newPassword: '',
				rePassword: '',
				code: {
					text: '获取验证码',
					status: false,
					value: ''
				},
				username: '',
				session:''
			}
		},
		methods: {
			onShow() {
				this.username = uni.getStorageSync('username');
				this.session = uni.getStorageSync('token');
			},
			loginOut(){
				uni.clearStorage();
				uni.showToast({
					icon:'none',
					title:'退出登录成功'
				})
				uni.navigateTo({
					url:'../login/login'
				})
				//清除缓存
				try{
					uni.clearStorageSync();
				} catch(e){
					
				}
			},
			radioGroupChange(e) {
				
			},
			password(){
				if (this.newPassword == "") {
					uni.showToast({
						icon: 'none',
						title: '新密码不能为空'
					})
					return;
				}
				if (this.newPassword != this.rePassword) {
					uni.showToast({
						icon: 'none',
						title: '新密码与重复密码不一致'
					})
					return;
				}
				PasswordRequest.postf(api.updatePassword, {
					session:this.session,
					username: this.username,
					oldPassword: this.oldPassword,
					newPassword: this.newPassword
				}).then(response => {
					if (response.data.code == 0) {
						uni.showToast({
							icon: 'success',
							title: '修改密码成功',
						})
						this.loginOut();
					}
					 if (response.data.code == 603) {
					 	uni.showToast({
					 		icon: 'none',
					 		title: '旧密码输入错误!',
					 	})
					 } else {
						uni.showToast({
							icon: 'none',
							title: '修改密码失败请与管理员联系'
						})
					}
				}).catch(ex => {
					uni.showToast({
						icon: 'none',
						title: '请求失败，请稍后再试'
					})
				})
			},
			modifypass: function() {
				request.getf(api.checkInputUserName, {
					username: this.username
				}).then(response => {
					 if (response.data.data == 'true') {
					 	this.password()
					 } else {
						uni.showToast({
							icon: 'none',
							title: '此用户不存在！'
						})
					}
				}).catch(ex => {
					uni.showToast({
						icon: 'none',
						title: '请求失败，请稍后再试'
					})
				})
			}
		}
	}
</script>

<style>
	.form {
		background-color: #FFFFFF;
		padding: 30rpx;
		width: 100%;

	}

	.code-btn {
		background: none;
		font-size: 28rpx;
		color: #845708;
		border-left: 1rpx solid #d0b17b;
		border-radius: 0;
		height: 42rpx;
		width: 190rpx;
		padding: 0;

	}

	.cu-btn {
		position: relative;
		border: 0upx;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
		padding: 0 30upx;
		font-size: 28upx;
		height: 64upx;
		line-height: 1;
		text-align: center;
		text-decoration: none;
		overflow: visible;
		margin-left: initial;
		transform: translate(0upx, 0upx);
		margin-right: initial;
	}
</style>
